package com.moxiu.browser;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.Drawable;
import android.util.Log;
//import android.telecom.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.DecelerateInterpolator;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.moxiu.browser.mainactivity.B_BrowserMainManager;
import com.moxiu.browser.menu.DesktopMenuAdapter;
import com.moxiu.browser.menu.DesktopMenuAdapter.ViewHolder;
import com.moxiu.browser.preferences.BrowserAlcPost;
import com.moxiu.browser.view.DesktopMenuGridView;
import com.moxiu.browser.view.NightToast;
import com.moxiu.launcher.R;

public class BottomNavigation extends RelativeLayout implements OnClickListener {
	private static final float BOTTOM_FORBID_BG_ALPHA = 0.3f;

	private static final float BOTTOM_PRESS_BG_ALPHA = 0.5f;
	private UiController mUiController;
	private BaseUi mBaseUi;
	private ViewGroup mContentView;
	private BrowserActivity mActivity;

	private ImageView mPrePageView;
	private View mNextPageView;
	private ImageView mMoreView;
	private View mMutiTabView;
	private View mHomePageViewBtn;
	private TextView mMultiTabNumView;

	private View mMultiTabLayout;
	boolean mAnimating;

	private Boolean Night=false;
	private BrowserAlcPost browserAlcPost;
	private Boolean isSendBrowse = true;

	public BottomNavigation(BrowserActivity context, UiController controller, BaseUi ui, ViewGroup contentView) {
		super(context, null);
		browserAlcPost = new BrowserAlcPost(context);
		mActivity = context;
		mUiController = controller;
		mBaseUi = ui;
		mContentView = contentView;
		initLayout(context);
	}

	private void initLayout(Context context) {
		LayoutInflater factory = LayoutInflater.from(context);
		factory.inflate(R.layout.br_bottom_nav_menu_layout, this);
		mContentView.addView(this, makeLayoutParams());
		
        SharedPreferences pferences = mActivity.getSharedPreferences("default_night",
					mActivity.MODE_PRIVATE);
	     Night=pferences.getBoolean("default_night", false);
		
		initBottomView();
		initMenu();

	}

	public void hideBottomNav() {
		if (getVisibility() == View.VISIBLE) {
			setVisibility(View.GONE);
			mContentView.setVisibility(View.GONE);
		}
	}
	
	public void showBottomNav() {
		if (getVisibility() != View.VISIBLE) {
			setVisibility(View.VISIBLE);
			mContentView.setVisibility(View.VISIBLE);
		}
	}
	private ViewGroup.LayoutParams makeLayoutParams() {
		return new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
	}

	private void initBottomView() {
		mPrePageView = (ImageView) findViewById(R.id.b_pre_page);// .setOnClickListener(this);
		mNextPageView = findViewById(R.id.b_next_page);// .setOnClickListener(this);
		mMoreView = (ImageView) findViewById(R.id.b_more);
		mMutiTabView = findViewById(R.id.b_multi_tab);
		mHomePageViewBtn = findViewById(R.id.b_home);
		mMultiTabNumView = (TextView) findViewById(R.id.b_multi_tab_num);

		mMultiTabLayout = findViewById(R.id.br_bottom_multi_layout);
		
		changeMulitTabNumber();

		mPrePageView.setOnClickListener(this);
		mNextPageView.setOnClickListener(this);
		mMoreView.setOnClickListener(this);
		mMutiTabView.setOnClickListener(this);
		mHomePageViewBtn.setOnClickListener(this);

	}

	private DesktopMenuGridView menuGridView;
	private View mMenuLayout;
	DesktopMenuAdapter bodyAdapter;
	private View menu_grayBackView;

	private void initMenu() {

		mMenuLayout = findViewById(R.id.bottom_menu);
		menuGridView = (DesktopMenuGridView) (mMenuLayout.findViewById(R.id.bootom_menu_gv));
		menu_grayBackView = mMenuLayout.findViewById(R.id.br_menu_view);
		menu_grayBackView.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
//				hideBottomMenu();
				menuColoseAnim(true);

			}
		});
		boolean isNoImg = !mUiController.getSettings().loadImages();
		int[] resIds = new int[] { R.drawable.br_menu_bookhistory, R.drawable.br_menu_add_bookmark,
				R.drawable.br_menu_share, R.drawable.br_menu_setting, R.drawable.br_menu_feedback,
				R.drawable.br_menu_no_img, R.drawable.br_menu_refresh, R.drawable.br_menu_exit };
		int[] resSelectIds = new int[] { R.drawable.br_menu_bookhistory, R.drawable.br_menu_add_bookmark,
				R.drawable.br_menu_share, R.drawable.br_menu_setting, R.drawable.br_menu_feedback,
				R.drawable.br_menu_have_img, R.drawable.br_menu_refresh, R.drawable.br_menu_exit };

		boolean[] selected = new boolean[] { false, false, false, false, false, isNoImg, false, false };

		Resources res = mActivity.getResources();
		bodyAdapter = new DesktopMenuAdapter(mActivity, resIds, resSelectIds,
				new String[] { res.getString(R.string.br_menu_bookhistory),
						res.getString(R.string.br_menu_addbookmarks), res.getString(R.string.br_menu_share),
						res.getString(R.string.br_menu_settings), res.getString(R.string.br_menu_feedback),
						res.getString(R.string.br_menu_noimg), res.getString(R.string.br_menu_opennight),
						res.getString(R.string.br_menu_exit)

		}, selected);

		menuGridView.setAdapter(bodyAdapter);
		menuGridView.setOnItemClickListener(new DesktopMenuClickListener());
	}

	class DesktopMenuClickListener implements OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {

			DesktopMenuAdapter.ViewHolder holder = (ViewHolder) view.getTag();
			if (holder.type != DesktopMenuAdapter.TYPE_CHANGE_IMG) {
				if (holder.selected) {
					return;
				}
			}

			switch (position) {
			/**  **/
			case 0:
				showBookmarksHistroy();
				browserAlcPost.setBrowserData(browserAlcPost.MENU_CATEGORY, browserAlcPost.MENU2_BM_HT);
				browserAlcPost.postMenuClickData(BrowserAlcPost.MENU_SET, BrowserAlcPost.MENU2_BM_HT);
				break;
			/**  **/
			case 1:
				addBookmarks();
				browserAlcPost.setBrowserData(browserAlcPost.MENU_CATEGORY, browserAlcPost.MENU2_ADD_BM);
				browserAlcPost.postMenuClickData(BrowserAlcPost.MENU_SET, BrowserAlcPost.MENU2_ADD_BM);
				break;
			/** **/
			case 2:
				share();
				browserAlcPost.setBrowserData(browserAlcPost.MENU_CATEGORY, browserAlcPost.MENU2_SHARE);
				browserAlcPost.postMenuClickData(BrowserAlcPost.MENU_SET, BrowserAlcPost.MENU2_SHARE);
				break;
			/**  **/
			case 3:
				settings();
				browserAlcPost.setBrowserData(browserAlcPost.MENU_CATEGORY, browserAlcPost.MENU2_SET);
				browserAlcPost.postMenuClickData(BrowserAlcPost.MENU_SET, BrowserAlcPost.MENU2_SET);
				break;

			case 4:
				feedback();
				browserAlcPost.setBrowserData(browserAlcPost.MENU_CATEGORY, browserAlcPost.MENU2_FEEDBACK);
				browserAlcPost.postMenuClickData(BrowserAlcPost.MENU_SET, BrowserAlcPost.MENU2_FEEDBACK);
				break;

			case 5:
				boolean selected = mUiController.getSettings().loadImages();
				bodyAdapter.setSelectChange(5, selected);
				String tip = selected ? "无图模式已开启" : "无图模式已关闭";
				NightToast.makeText(mActivity, tip, Toast.LENGTH_SHORT).show();
				noImgMode(!mBaseUi.getActiveTab().isShowingHomePage());
				browserAlcPost.setBrowserData(browserAlcPost.MENU_CATEGORY, browserAlcPost.MENU2_NO_IMG);
				browserAlcPost.postMenuClickData(BrowserAlcPost.MENU_SET, BrowserAlcPost.MENU2_NO_IMG);
				break;
			/**  **/
			case 6:

				browserAlcPost.setBrowserData(browserAlcPost.MENU_CATEGORY, browserAlcPost.MENU2_NIGHT);
				browserAlcPost.postMenuClickData(BrowserAlcPost.MENU_SET, BrowserAlcPost.MENU2_NIGHT);
				refreshNightModel();
				 // refresh();
				break;
			/**  **/
			case 7:
				browserAlcPost.setBrowserData(browserAlcPost.MENU_CATEGORY, browserAlcPost.MENU2_EXIT);
				browserAlcPost.postMenuClickData(BrowserAlcPost.MENU_SET, BrowserAlcPost.MENU2_EXIT);
				exitBrowser();
				break;
			default:
				break;
			}
			menuColoseAnim(true);
		}

	}

    public void refreshNightModel(){
    
    	mUiController.refreshNightModel();
    }
	
	public void showBookmarksHistroy() {
		// TODO Auto-generated method stub
		mUiController.showBookHistory();
	}

	public void exitBrowser() {
		// TODO Auto-generated method stub
		mUiController.closeBrowser();
	}

	public void refresh() {
		// TODO Auto-generated method stub
		mUiController.refreshWeb();
	}

	public void noImgMode(boolean needRefresh) {
		// TODO Auto-generated method stub
		mUiController.openNoImg();
		if (needRefresh) {
			mUiController.refreshWeb();
		}
	}

	public void feedback() {
		// TODO Auto-generated method stub
		mUiController.openFeedBack();
	}

	public void settings() {
		// TODO Auto-generated method stub
		mUiController.openSettings();
	}

	public void share() {
		// TODO Auto-generated method stub
		mUiController.openShare();
	}

	public void addBookmarks() {
		// TODO Auto-generated method stub
		mUiController.addBookmarks();
	}
	
	public void onHomePageShowStateChange(boolean show) {
		if (show) {
    		bodyAdapter.setSelectChange(1, true);
			bodyAdapter.setSelectChange(2, true);
		   // bodyAdapter.setSelectChange(6, true);
			mUiController.forceScreenOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    	} else {
    		bodyAdapter.setSelectChange(1, false);
			bodyAdapter.setSelectChange(2, false);
			//bodyAdapter.setSelectChange(6, false);
			mUiController.forceScreenOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
    	}
    	bodyAdapter.notifyDataSetChanged();
	}

	public void onTabDataChanged(Tab tab) {
		if (tab == null || tab != mUiController.getCurrentTab()) {
			return;
		}
		if (tab != null) {
			changeBottomNavState(tab);			
		if (tab.canGoBack()) {
				mPrePageView.setClickable(true);			
				mPrePageView.setAlpha(1.0f);
				browserAlcPost.setBrowserData(BrowserAlcPost.IS_HOME, browserAlcPost.getIsHome(false));
			} else {
		        mPrePageView.setClickable(false);				
				mPrePageView.setAlpha(BOTTOM_PRESS_BG_ALPHA);				
				browserAlcPost.setBrowserData(BrowserAlcPost.IS_HOME, browserAlcPost.getIsHome(true));		
			}
			if (tab.canGoForward()) {
				mNextPageView.setClickable(true);
				mNextPageView.setAlpha(1.0f);
			} else {
				mNextPageView.setClickable(false);	
				
				mNextPageView.setAlpha(BOTTOM_PRESS_BG_ALPHA);					
							
			}

			changeMenuState(tab);

		} 
//		else {
//			mPrePageView.setAlpha(BOTTOM_FORBID_BG_ALPHA);
//		}

		

	}

	@Override
	public void onClick(View v) {
		browserAlcPost.isSettingShow = true;
		
		int id = v.getId();
		switch (id) {
		case R.id.b_pre_page:
			mUiController.goBackOnePage();
			browserAlcPost.setBrowserData(browserAlcPost.MENU, browserAlcPost.MENU_BACK);
			if (mBaseUi.mainManager.isSearchState) {
				if (mBaseUi.mainManager.searchisFromHome) {
					browserAlcPost.postPrePageClick(BrowserAlcPost.B_CLICK, BrowserAlcPost.MENU_BACK, browserAlcPost.getIsHome(true));
					isSendBrowse = false;
				}else {
					browserAlcPost.postPrePageClick(BrowserAlcPost.B_CLICK, BrowserAlcPost.MENU_BACK, browserAlcPost.getIsHome(false));
					isSendBrowse = true;
				}
			}else {
				browserAlcPost.postPrePageClick(BrowserAlcPost.B_CLICK, BrowserAlcPost.MENU_BACK, browserAlcPost.getIsHome(false));
				isSendBrowse = true;
			}
			if (mUiController.getCurrentTab().canGoBack()) {
				browserAlcPost.setBrowserData(BrowserAlcPost.IS_HOME, browserAlcPost.getIsHome(false));
			}else {
				browserAlcPost.setBrowserData(BrowserAlcPost.IS_HOME, browserAlcPost.getIsHome(true));
				if (isSendBrowse) {
					browserAlcPost.sendBrowseMessage(true, true);
				}
			}
			// if (getActiveTab() != null && getActiveTab().canGoBack()) {
			// getActiveTab().goBack();
			// }
			// mPrePageView.setAlpha(BOTTOM_PRESS_BG_ALPHA);
			break;
		case R.id.b_next_page:
			if (mBaseUi.getActiveTab() != null && mBaseUi.getActiveTab().canGoForward()) {
				browserAlcPost.setBrowserData(browserAlcPost.MENU, browserAlcPost.MENU_GO);
				if (mUiController.getCurrentTab().canGoBack()) {
					if (mBaseUi.mainManager.isSearchState) {
						if (mBaseUi.mainManager.searchisFromHome) {
							browserAlcPost.postPrePageClick(BrowserAlcPost.B_CLICK, BrowserAlcPost.MENU_GO, browserAlcPost.getIsHome(true));
							browserAlcPost.setBrowserData(BrowserAlcPost.IS_HOME, browserAlcPost.getIsHome(true));
						}else {
							browserAlcPost.postPrePageClick(BrowserAlcPost.B_CLICK, BrowserAlcPost.MENU_GO, browserAlcPost.getIsHome(false));
							browserAlcPost.setBrowserData(BrowserAlcPost.IS_HOME, browserAlcPost.getIsHome(false));
						}
					}else {
						browserAlcPost.postPrePageClick(BrowserAlcPost.B_CLICK, BrowserAlcPost.MENU_GO, browserAlcPost.getIsHome(false));
						browserAlcPost.setBrowserData(BrowserAlcPost.IS_HOME, browserAlcPost.getIsHome(false));
					}
				}else {
					browserAlcPost.postPrePageClick(BrowserAlcPost.B_CLICK, BrowserAlcPost.MENU_GO, browserAlcPost.getIsHome(true));
				}
				mBaseUi.getActiveTab().goForward();
//				browserAlcPost.postPrePageClick(BrowserAlcPost.B_CLICK, BrowserAlcPost.MENU_GO, browserAlcPost.getIsHome(false));
			}
			break;
		case R.id.b_more:
			showMenu();
			browserAlcPost.setBrowserData(browserAlcPost.ACT_TYPE, browserAlcPost.B_CLICK);
//			browserAlcPost.setBrowserData(BrowserAlcPost.IS_HOME, BrowserAlcPost.IN_HOME);
			browserAlcPost.setBrowserData(browserAlcPost.MENU, browserAlcPost.MENU_SET);
			if (mUiController.getCurrentTab().canGoBack()) {
				if (mBaseUi.mainManager.isSearchState) {
					if (mBaseUi.mainManager.searchisFromHome) {
						browserAlcPost.postPrePageClick(BrowserAlcPost.B_CLICK, BrowserAlcPost.MENU_SET, browserAlcPost.getIsHome(true));
						browserAlcPost.setBrowserData(BrowserAlcPost.IS_HOME, browserAlcPost.getIsHome(true));
					}else {
						browserAlcPost.postPrePageClick(BrowserAlcPost.B_CLICK, BrowserAlcPost.MENU_SET, browserAlcPost.getIsHome(false));
						browserAlcPost.setBrowserData(BrowserAlcPost.IS_HOME, browserAlcPost.getIsHome(false));
					}
				}else {
					browserAlcPost.postPrePageClick(BrowserAlcPost.B_CLICK, BrowserAlcPost.MENU_SET, browserAlcPost.getIsHome(false));
					browserAlcPost.setBrowserData(BrowserAlcPost.IS_HOME, browserAlcPost.getIsHome(false));
				}
			}else {
				browserAlcPost.postPrePageClick(BrowserAlcPost.B_CLICK, BrowserAlcPost.MENU_SET, browserAlcPost.getIsHome(true));
				browserAlcPost.setBrowserData(BrowserAlcPost.IS_HOME, browserAlcPost.getIsHome(true));
			}
//			browserAlcPost.postAlcData(mContext);
//			mBaseUi.showOnlyFixTile(true);
			break;
		case R.id.b_multi_tab:
			browserAlcPost.setBrowserData(browserAlcPost.ACT_TYPE, browserAlcPost.B_CLICK);
			browserAlcPost.setBrowserData(browserAlcPost.MENU, browserAlcPost.MENU_PAGE);
			if (mUiController.getCurrentTab().canGoBack()) {
				if (mBaseUi.mainManager.isSearchState) {
					if (mBaseUi.mainManager.searchisFromHome) {
						browserAlcPost.postPrePageClick(BrowserAlcPost.B_CLICK, BrowserAlcPost.MENU_PAGE, browserAlcPost.getIsHome(true));
					}else {
						browserAlcPost.postPrePageClick(BrowserAlcPost.B_CLICK, BrowserAlcPost.MENU_PAGE, browserAlcPost.getIsHome(false));
					}
				}else {
					browserAlcPost.postPrePageClick(BrowserAlcPost.B_CLICK, BrowserAlcPost.MENU_PAGE, browserAlcPost.getIsHome(false));
				}
			}else {
				browserAlcPost.postPrePageClick(BrowserAlcPost.B_CLICK, BrowserAlcPost.MENU_SET, browserAlcPost.getIsHome(true));
				browserAlcPost.setBrowserData(BrowserAlcPost.IS_HOME, browserAlcPost.getIsHome(true));
			}
//			browserAlcPost.postAlcData(mContext);
			((PhoneUi)mBaseUi).showNavScreen();
			break;
		case R.id.b_home:
			// mUiController.loadUrl(getActiveTab(),
			// mUiController.getSettings().getHomePage());
//			browserAlcPost.setBrowserData(BrowserAlcPost.IS_HOME, browserAlcPost.getIsHome(true));
			browserAlcPost.setBrowserData(browserAlcPost.ACT_TYPE, browserAlcPost.B_CLICK);
			browserAlcPost.setBrowserData(browserAlcPost.MENU, browserAlcPost.MENU_HOME);
//			browserAlcPost.postAlcData(mContext);
			if (mUiController.getCurrentTab().canGoBack()) {
				if (mBaseUi.mainManager.isSearchState) {
					if (mBaseUi.mainManager.searchisFromHome) {
						browserAlcPost.postPrePageClick(BrowserAlcPost.B_CLICK, BrowserAlcPost.MENU_HOME, browserAlcPost.getIsHome(true));
						isSendBrowse = false;
					}else {
						browserAlcPost.postPrePageClick(BrowserAlcPost.B_CLICK, BrowserAlcPost.MENU_HOME, browserAlcPost.getIsHome(false));
						isSendBrowse = true;
					}
				}else {
					browserAlcPost.postPrePageClick(BrowserAlcPost.B_CLICK, BrowserAlcPost.MENU_HOME, browserAlcPost.getIsHome(false));
					isSendBrowse = true;
				}
			}else {
				browserAlcPost.postPrePageClick(BrowserAlcPost.B_CLICK, BrowserAlcPost.MENU_HOME, browserAlcPost.getIsHome(true));
			}
//			browserAlcPost.setBrowserData(BrowserAlcPost.IS_HOME, browserAlcPost.getIsHome(true));
			Tab tab = mBaseUi.getActiveTab();
			if (tab != null) {
				if (!tab.isShowingHomePage()) {
					 mBaseUi.isHideTitle(true,false);
					tab.showHomePage();
					tab.mainManager.showHomePage();
					tab.mainManager.clearUrlInput();
//					browserAlcPost.setBrowserData(browserAlcPost.ACT_TYPE, browserAlcPost.B_BROWSE);
//					browserAlcPost.isHomeClicked = true;
//					browserAlcPost.postAlcData(mActivity);
				}else{
					tab.mainManager.showHomePage();
					tab.mainManager.clearUrlInput();
				}
			}
			mBaseUi.getHomePage().onResume(0);
			if (isSendBrowse) {
				browserAlcPost.sendBrowseMessage(true, true);
			}
//			browserAlcPost.sendBrowseMessage(true, true);
			break;
		default:
			break;
		}
	}
	
	public void showMenu() {
		if (mAnimating) {
			return;
		}
		
		if (isMenuShowing()) {
			mMoreView.setImageResource(R.drawable.br_bottom_setting);
			menuColoseAnim(true);
		} else {
			mMoreView.setImageResource(R.drawable.br_menu_show_green);
			menuStartAnim(true);
		}
		
	}
	public boolean isMenuShowing() {
		return mMenuLayout.getVisibility() == View.VISIBLE;
	}
	public void changeMulitTabNumber() {
		int tabs = 0;
		if ( mUiController.getTabs() != null) {
			tabs = mUiController.getTabs().size();
		}
		
		if (tabs > 0) {
			mMultiTabNumView.setText(""+tabs);
			mMultiTabNumView.setVisibility(View.VISIBLE);
		} else {
			mMultiTabNumView.setText("");
			mMultiTabNumView.setVisibility(View.GONE);
		}
	}
	

	 private void changeMenuState(Tab tab) {
	    	if (tab.isShowingHomePage()) {
				bodyAdapter.setSelectChange(1, true);
				bodyAdapter.setSelectChange(2, true);
				//bodyAdapter.setSelectChange(6, true);
				
			} else {
				bodyAdapter.setSelectChange(1, false);
				bodyAdapter.setSelectChange(2, false);
				//bodyAdapter.setSelectChange(6, false);
			}
	    	bodyAdapter.notifyDataSetChanged();
	    }
	
	
	private void setOtherBottomViewsVisble(int visible) {
		mPrePageView.setVisibility(visible);
		mNextPageView.setVisibility(visible);
		mMutiTabView.setVisibility(visible);
		mHomePageViewBtn.setVisibility(visible);
		mMultiTabLayout.setVisibility(visible);
	}
	
	/**
	 * 
	 */
	private void menuStartAnim(boolean isAnim) {
		
		if (isAnim) {
			   /**加载透明动画**/
				final AlphaAnimation animation = new AlphaAnimation(0, 0.7f);
				animation.setDuration(100);//设置动画持续时间 
				animation.setStartOffset(150);//执行前的等待时间 
				animation.setInterpolator(new DecelerateInterpolator());
			    /**播放透明动画**/
				animation.setFillAfter(true);//动画执行完后是否停留在执行完的状态 
			    menu_grayBackView.startAnimation(animation);  
			
			ValueAnimator valueAnimator = ValueAnimator.ofFloat(0f, 1f);
			valueAnimator.setDuration(250);
			valueAnimator.setInterpolator(new DecelerateInterpolator());
			final int desktopMenuAnimOffsetY = mMenuLayout.getHeight();
//					getResources()
//					.getDimensionPixelSize(R.dimen.desktopMenuAnimOffsetY);
			valueAnimator.addListener(new AnimatorListenerAdapter() {
				@Override
				public void onAnimationStart(Animator animation) {
					mAnimating = true;
					// TODO Auto-generated method stub
					mMenuLayout.setAlpha(0.01f);
					mMenuLayout.setVisibility(View.VISIBLE);
					
					setOtherBottomViewsVisble(View.INVISIBLE);
				}
				
				@Override
				public void onAnimationEnd(Animator animation) {
//					mCustomScreenLinearLayout.invalidate();
					BottomNavigation.this.invalidate();
					mAnimating = false;
				}
			});
			valueAnimator
					.addUpdateListener(new LauncherAnimatorUpdateListener() {

						@Override
						public void onAnimationUpdate(float a, float b) {
							// TODO Auto-generated method stub
							
							mMenuLayout.setAlpha(b);
							mMenuLayout.setTranslationY(a
									* desktopMenuAnimOffsetY);
						}
					});
			valueAnimator.start();

		} else {
			mMenuLayout.setVisibility(View.VISIBLE);
		}

		

	}
	
	/**
	 * 关闭桌面设置
	 */
	public void menuColoseAnim(boolean isAnim) {

		browserAlcPost.isSettingShow = false;
		if(Night){
			   mMoreView.setImageDrawable(getDyeDrawable(mActivity,R.drawable.br_bottom_setting,true));
				}else{
					getDyeDrawable(mActivity,R.drawable.br_bottom_setting,false);
					mMoreView.setImageResource(R.drawable.br_menu_setting_click);	
				}
		
		if (isAnim) {
			/**加载透明动画**/
			final AlphaAnimation animation = new AlphaAnimation(0.7f,0);
			animation.setDuration(1);//设置动画持续时间 
		    /**播放透明动画**/
			animation.setFillAfter(true);//动画执行完后是否停留在执行完的状态 
		    menu_grayBackView.startAnimation(animation);  
			ValueAnimator valueAnimator = ValueAnimator.ofFloat(0f, 1f);
			valueAnimator.setDuration(250);
			valueAnimator.setInterpolator(new DecelerateInterpolator(2));
//			final int desktopMenuAnimOffsetY = getResources()
//					.getDimensionPixelSize(R.dimen.desktopMenuAnimOffsetY);
			final int desktopMenuAnimOffsetY = mMenuLayout.getHeight();
			
			valueAnimator.addListener(new AnimatorListenerAdapter() {
				@Override
				public void onAnimationStart(Animator animation) {
					// TODO Auto-generated method stub
					mMenuLayout.setAlpha(1f);
					mAnimating = true;
					setOtherBottomViewsVisble(View.VISIBLE);
				}

				@Override
				public void onAnimationEnd(Animator animation) {
					// TODO Auto-generated method stub
					mMenuLayout.setVisibility(View.GONE);
//					mCustomScreenLinearLayout.invalidate();
					BottomNavigation.this.invalidate();
					mAnimating = false;
				}

			});
			valueAnimator
					.addUpdateListener(new LauncherAnimatorUpdateListener() {

						@Override
						public void onAnimationUpdate(float a, float b) {
							// TODO Auto-generated method stub
							mMenuLayout.setAlpha(a);
							mMenuLayout.setTranslationY(b
									* desktopMenuAnimOffsetY);
						}
					});
			valueAnimator.start();
		} else {
//			default_launcher_btn.setVisibility(View.INVISIBLE);
			mMenuLayout.setVisibility(View.INVISIBLE);
		}

	}
	void changeBottomNavState(Tab tab) {
   	 if (mPrePageView != null) {
  		 
        	if (tab.isAppTab() && tab.closeOnBack() && (tab.getWebView() != null && !tab.getWebView().canGoBack())) {
        		if(Night){
        			mPrePageView.setImageDrawable(getDyeDrawable(mActivity,R.drawable.br_bottom_back_from_otherapp_btn_normal,true));			
        		}else{
        			getDyeDrawable(mActivity,R.drawable.br_bottom_back_from_otherapp_btn_normal,false);
        		mPrePageView.setImageDrawable(mActivity.getResources().getDrawable(R.drawable.br_bottom_back_from_otherapp_btn));
        		    }
        	} else {
        		if(Night){
        			 mPrePageView.setImageDrawable(getDyeDrawable(mActivity,R.drawable.br_bottom_pre_page,true));	
        		}else{       	
        		mPrePageView.setImageDrawable(mActivity.getResources().getDrawable(R.drawable.br_menu_back_click));
        		}
        		
        	}
        }
   }
	public abstract class LauncherAnimatorUpdateListener implements AnimatorUpdateListener {
	    public void onAnimationUpdate(ValueAnimator animation) {
	        final float b = (Float) animation.getAnimatedValue();
	        final float a = 1f - b;
	        onAnimationUpdate(a, b);
	    }

	    public abstract void onAnimationUpdate(float a, float b);
	}
	
	public void NightModel(Boolean isNight){
		if(isNight){    
			Night=true;		         
		         ((ImageView) mPrePageView).setImageDrawable(getDyeDrawable(mActivity,R.drawable.br_bottom_pre_page,true));	        	
		      	 ((ImageView) mNextPageView).setImageDrawable(getDyeDrawable(mActivity,R.drawable.br_bottom_next_page,true));		       
			         mMoreView.setImageDrawable(getDyeDrawable(mActivity,R.drawable.br_bottom_setting,true));	        
			         ((ImageView) mMutiTabView).setImageDrawable(getDyeDrawable(mActivity,R.drawable.br_bottom_mulit_tab,true));		        
			         ((ImageView) mHomePageViewBtn).setImageDrawable(getDyeDrawable(mActivity,R.drawable.br_bottom_home_page,true));			         
		         mMultiTabNumView.setTextColor(Color.WHITE);		       	         	      	     
	    	     bodyAdapter.isNight=true;
	    	     bodyAdapter.notifyDataSetChanged();	    	     
		}else{
			Night=false;
			getDyeDrawable(mActivity,R.drawable.br_bottom_pre_page,false);
			getDyeDrawable(mActivity,R.drawable.br_bottom_next_page,false);
			getDyeDrawable(mActivity,R.drawable.br_bottom_setting,false);
			getDyeDrawable(mActivity,R.drawable.br_bottom_mulit_tab,false);
			getDyeDrawable(mActivity,R.drawable.br_bottom_home_page,false);
			
			 ((ImageView) mPrePageView).setImageResource(R.drawable.br_menu_back_click);		        	
	    	 ((ImageView) mNextPageView).setImageResource(R.drawable.br_menu_forward_click);		       
		         mMoreView.setImageResource(R.drawable.br_menu_setting_click);		        
		         ((ImageView) mMutiTabView).setImageResource(R.drawable.br_menu_multi_click);	        		       
		         ((ImageView) mHomePageViewBtn).setImageResource(R.drawable.br_menu_home_click); 		         
		         mMultiTabNumView.setTextColor(Color.BLACK);	
		         bodyAdapter.isNight=false;
	    	     bodyAdapter.notifyDataSetChanged();
		}
	}
	public  Drawable getDyeDrawable(Context context, int drawableId,Boolean isNight) {
		Drawable drawable = null;
		drawable = context.getResources().getDrawable(drawableId);    
        try {   
        	if(isNight){        	   	
                drawable.setColorFilter(Color.WHITE, Mode.SRC_IN);
                }else{
                drawable.setColorFilter(Color.BLACK, Mode.SRC_IN);	
                }    	
                  
        } catch (OutOfMemoryError error) {
        }
		return drawable;
	}
}
